package algorithm.ldk.LinearSearch;

/**
 * @作者：ldk
 * @email 2505496685@qq.com
 * @描述：默认描述
 * @创建时间：9:44 2020-12-29
 * @修改时间：9:44 2020-12-29
 * @修改描述：默认描述
 */

/**
 * 线性查找法
 */
public class LinearSearch {

    private LinearSearch(){}

    //查找方法
    public static <E> int search(E[] data,E target){
        for(int i = 0; i < data.length; i++){
            if(data[i].equals(target) ){
                return i;
            }
        }
        return -1;
    }


    public static void main(String[] args){

//        Integer[] data = {24,18,12,9,16,66,32,4};
//        int result = LinearSearch.search(data, 16);
//        System.out.println(result);
//p0
//        int res = LinearSearch.search(data, 65);
//        System.out.println(res);
//
//        Student[] students = {new Student("Alice"),
//                            new Student("Bobo"),
//                            new Student("kqq")};
//
//        Student kqq = new Student("kqq");
//        int search = LinearSearch.search(students, kqq);
//        System.out.println(search);

        int[] dataSize = {1000000,10000000};
        for (int n : dataSize) {
            //        int n = 1000000;
            Integer[] data = ArrayGenerator.generateOrderedArray(n);

            long startTime = System.nanoTime();
            for (int k = 0; k < 100; k++) {
                LinearSearch.search(data,n);
            }
            long endTime = System.nanoTime();

            double time = (endTime - startTime) / 1000000000.0;
            System.out.println("n = " + n + ",100 runs:" + time + " s");
        }

    }
}
